﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufParameterPreviewUtils.h"
#include "GbmpQuickCast.h"
#include "IApplicationWindow.h"
#include "IDocument.h"
#include "IElementBasicInformation.h"
#include "IFamily.h"
#include "IInstanceType.h"
#include "IType.h"
#include "IUiManager.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

namespace gfam
{
    const IElementBasicInformation* GnufParameterPreviewUtils::GetFamilyInfoByType(gcmp::IDocument* pDoc, const gcmp::ElementId& typeId)
    {
        IType* pickedType = quick_cast<IType>(pDoc->GetElement(typeId));

        if (pickedType == nullptr)
            return nullptr;

        const IInstanceType* pInstanceType = quick_cast<IInstanceType>(pickedType);
        if (!pInstanceType)
            return nullptr;

        const IFamily* pFamily = quick_cast<IFamily>(pInstanceType->GetFamily());
        if (!pFamily)
            return nullptr;

        return pFamily->GetBasicInformation();
    }

    const std::wstring GnufParameterPreviewUtils::GetFamilyName(gcmp::IDocument* pDoc, const gcmp::ElementId& typeId)
    {
        const IElementBasicInformation* pInfo = GetFamilyInfoByType(pDoc, typeId);
        if (!pInfo)
            return L"";
        return pInfo->GetName();
    }

    std::tuple<bool, ePreviewType> GnufParameterPreviewUtils::IsCanPreviewParameter(gcmp::IDocument*pDoc, const gcmp::ElementId& typeId)
    {
        const std::wstring& familyName = GetFamilyName(pDoc, typeId);
        bool canPreview = false;
        ePreviewType previewType = ePreviewType::eNone;
        for (auto& typeInfo : allNeedCheckFamilyInfo)
        {
            if (std::get<0>(typeInfo) == familyName)
            {
                canPreview = true;
                previewType = std::get<3>(typeInfo);
                break;
            }
        }
        return std::move(std::make_tuple(canPreview, previewType));
    }

    //暂定参数展示窗口以主程序窗口作为父窗口
    QWidget* GnufParameterPreviewUtils::GetParentWidget()
    {
        IUiManager* pUiManager = IUiManager::Get();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiManager, L"pUiManager为空!", L"paramodel", L"2024-02-20");
        IApplicationWindow* pAppMainWindow = pUiManager->GetApplicationWindow();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pAppMainWindow, L"pAppMainWindow为空!", L"paramodel", L"2024-02-20");
        QWidget * pWidget = QWidget::find(WId(pAppMainWindow->GetApplicationWindowId()));
        return pWidget;
    }
}